package com.xw.controller.api.sysapi;

import com.google.common.collect.Maps;
import com.xw.common.Const;
import com.xw.service.UserInfoService;
import com.xw.service.sysconfig.UserService;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.util.JsonUtil;
import com.xw.vo.UserInfoVo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/api/user/")
@Scope("prototype")
public class ApiUserController {

    @Resource
    private UserService userService;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 微信登录 (通过微信授权拿到相应的手机号，根据手机号去匹配库中数据)
     * @param userPhone     手机号
     * @param userNickName  昵称
     * @param userImg       头像
     * @return
     */
    @RequestMapping(value = "loginByWX", method = RequestMethod.POST)
    @ResponseBody
    public Object loginByWX(String userPhone, String userNickName, String userImg) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userPhone", userPhone);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = userService.loginByWX(userPhone,userNickName,userImg);
        return map;
    }

    /**
     * 修改性别
     * @param request
     * @param uiId
     * @param gender
     * @return
     */
    @RequestMapping(value = "updateGenderByUiId", method = RequestMethod.POST)
    @ResponseBody
    public Object updateGenderByUiId(HttpServletRequest request, String uiId,Integer gender) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("uiId", uiId);
        map.put("gender", gender);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = userService.updateGenderByUiId(uiId,gender);
        return map;
    }

    /**
     * 修改头像和昵称
     * @param request
     * @param userId
     * @param userImg
     * @param userNickName
     * @return
     */
    @RequestMapping(value = "updateUserByUserId", method = RequestMethod.POST)
    @ResponseBody
    public Object updateUserByUserId(HttpServletRequest request, String userId,String userImg,String userNickName) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = userService.updateUserByUserId(userId,userImg,userNickName);
        return map;
    }

    /**
     * 查询个人信息
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping(value = "findUserByUserId", method = RequestMethod.POST)
    @ResponseBody
    public Object findUserByUserId(HttpServletRequest request, String userId) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = userService.findUserByUserId(userId);
        return map;
    }


    /**
     * 判断手机号是否注册
     *
     * @param request
     * @param phone
     * @return
     */
    @RequestMapping(value = "phoneIsExist", method = RequestMethod.POST)
    @ResponseBody
    public Object phoneIsExist(HttpServletRequest request, String phone) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("phone", phone);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = userService.phoneIsExist(phone);
        return map;
    }

    /**
     * 手机密码登录
     *
     * @param phone 手机号
     * @param pwd   密码
     * @return
     */
    @RequestMapping(value = "loginByPwd", method = RequestMethod.POST)
    @ResponseBody
    public Object loginByPwd(HttpServletRequest request, String phone, String pwd) {

        Map<String, Object> map = Maps.newHashMap();
        map.put("phone", phone);
        map.put("pwd", pwd);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        map.clear();

        map = userService.loginByPwd(phone, pwd);

        return map;
    }


    /**
     * 手机验证码登录
     *
     * @param request
     * @param phone
     * @param vcId
     * @param codeNum
     * @return
     */
    @RequestMapping(value = "loginByCode", method = RequestMethod.POST)
    @ResponseBody
    public Object loginByCode(HttpServletRequest request, String phone, String vcId, String codeNum) {
        //验证 web
        if (!AppUtil.checkWeb(request)) {
            return JsonData.fail(Const.CHECK_WEB_ERROR);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("phone", phone);
        map.put("vcId", vcId);
        map.put("codeNum", codeNum);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        map.clear();

        map = userService.loginByCode(phone, vcId, codeNum);

        return map;
    }

    /**
     * 手机验证码注册
     *
     * @param request
     * @param phone    手机号
     * @param vcId     短信唯一id
     * @param codeNum  验证码
     * @param pwd      密码    （需要加密）
     * @param againPwd 验证密码 （需要加密）
     * @return
     */
    @RequestMapping(value = "registerByEmailAndCode", method = RequestMethod.POST)
    @ResponseBody
    public Object registerByPhoneAndCode(HttpServletRequest request, String phone, String vcId, String codeNum, String pwd, String againPwd) {

        Map<String, Object> map = Maps.newHashMap();
        map.put("phone", phone);
        map.put("vcId", vcId);
        map.put("codeNum", codeNum);
        map.put("pwd", pwd);
        map.put("againPwd", againPwd);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        map.clear();

        map = userService.registerByPhoneAndCode(phone, vcId, codeNum, pwd, againPwd);

        return map;
    }


    /**
     * 找回密码
     *
     * @param vcId     验证码唯一id
     * @param codeNum  用户输入的验证码
     * @param pwd      密码(加密)
     * @param againPwd 再次输入密码(加密)
     * @param phone    手机号
     */
    @RequestMapping(value = "findPwd", method = RequestMethod.POST)
    @ResponseBody
    public Object findPwd(HttpServletRequest request, String phone, String vcId, String codeNum, String pwd, String againPwd) {

        Map<String, Object> map = Maps.newHashMap();
        map.put("phone", phone);
        map.put("vcId", vcId);
        map.put("codeNum", codeNum);
        map.put("pwd", pwd);
        map.put("againPwd", againPwd);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        map.clear();

        map = userService.findPwd(phone, vcId, codeNum, pwd, againPwd);

        return map;
    }


    /**
     * @需求描述:添加/修改用户信息
     * @创建人:whh
     * @创建时间: 2019/11/15
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "changeUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public Object changeUserInfoAction(HttpServletRequest request,@RequestBody Map<String,Object> paramsMap)throws Exception{

        String str = AppUtil.checkIsNull(paramsMap);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        Map<String, Object> userInfomMap = (Map<String, Object>) paramsMap.get("userInfoVo");
        JsonUtil.mapToObject(userInfomMap, userInfoVo);


        if (null != userInfomMap.get("birth") && "" != userInfomMap.get("birth")){
            String birth = String.valueOf(userInfomMap.get("birth"));
            userInfoVo.setBirth(AppUtil.stringToDate(birth,"yyyy-MM-dd"));
        }
        //判断参数
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId",userInfoVo.getUserId());
        map.put("userNickName",userInfoVo.getUserNickName());
        map.put("Gender",userInfoVo.getGender());
        map.put("userArea",userInfoVo.getUserArea());
        map.put("userImg",userInfoVo.getUserImg());
        map.put("userJob",userInfoVo.getUserJob());
        map.put("userSdf",userInfoVo.getUserSdf());
        str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map.put("userInfoVo",userInfoVo);
        return userInfoService.changeUserInfo(map);
    }
}
